package database.service;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import database.auxiliary.LogGenerator;

import po.BookPO;

public class BookDataServiceSerializableImpl implements BookDataService {

	public ArrayList<BookPO> bookList;

	public BookDataServiceSerializableImpl() {
		bookList = new ArrayList<BookPO>();
	}

	@Override
	public void load() {
		try {
			File file = new File("data/book.ser");
			if (!file.exists()) {
				file.createNewFile();
			}
			ObjectInputStream oiStream = new ObjectInputStream(
					new FileInputStream(file));
			BookPO bookPO;
			while ((bookPO = (BookPO) oiStream.readObject()) != null) {
				bookList.add(bookPO);
			}
			LogGenerator.log("Book data load succeed");
		} catch (EOFException e) {
			save();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void save() {
		try {
			FileOutputStream fos = new FileOutputStream("data/book.ser");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			for (int i = 0; i < bookList.size(); i++) {
				oos.writeObject(bookList.get(i));
			}
			oos.writeObject(null);
			oos.close();
			LogGenerator.log("Book data save succeed");
		} catch (FileNotFoundException e) {
			System.out.println("File found failed!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public int insert(BookPO book) {
		book.setId(bookList.size() + 1);
		bookList.add(book);
		LogGenerator.log("Book insert: " + book.toString());
		return bookList.size();
	}

	@Override
	public void delete(BookPO book) {
		for (int i = 0; i < bookList.size(); i++) {
			if (bookList.get(i).equals(book)) {
				bookList.get(i).setId(-bookList.get(i).getId());
				LogGenerator.log("Book delete: " + book);
			}
		}
	}

	@Override
	public void update(BookPO book) {
		for (int i = 0; i < bookList.size(); i++) {
			if (book.getId() == bookList.get(i).getId()) {
				bookList.set(i, book);
				LogGenerator.log("Book update: " + book);
			}
		}
	}

	@Override
	public BookPO find(int id) {
		for (int i = 0; i < bookList.size(); i++) {
			if (id == bookList.get(i).getId()) {
				LogGenerator.log("Book find: " + bookList.get(i));
				return new BookPO(bookList.get(i));
			}
		}
		return null;
	}

	@Override
	public Iterator<BookPO> getIterator() {
		LogGenerator.log("Book return itr, length = " + bookList.size());
		return bookList.iterator();
	}

//	public static void main(String args[]) {
//		BookDataServiceSerializableImpl b = new BookDataServiceSerializableImpl();
//		b.load();
//		Iterator<BookPO> bookitr = b.getIterator();
//		while (bookitr.hasNext()) {
//			System.out.println(bookitr.next());
//		}
//	}

}
